package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.api.resolver.annotation.SsoStudent;
import com.lanou3g.ptp.api.resolver.annotation.SsoTeacher;
import com.lanou3g.ptp.api.service.ApiGradeService;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.grade.service.GradeRoleService;
import com.lanou3g.ptp.grade.service.GradeService;
import com.lanou3g.ptp.task.service.TaskService;
import com.lanou3g.ptp.utils.PtpResult;
import com.lanou3g.ptp.utils.PtpResultUtils;
import com.lanou3g.ptp.vo.GradeInfo;
import com.lanou3g.ptp.vo.MyGradesParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.InputStream;
import java.util.List;

@Api(description = "成绩接口")
@RestController
public class GradeController {
    @Reference
    private GradeService gradeService;
    @Reference
    private GradeRoleService gradeRoleService;
    @Reference
    private TaskService taskService;
    @Autowired
    private ApiGradeService apiGradeService;

    @ApiOperation("学生/老师根据taskId获取自己相关成绩，包括自己打的分数和打给自己的分数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/mGrades")
    public PtpResult getMyGrades(MyGradesParam myGradesParam,
                                 @ApiIgnore @SsoTeacher Teacher teacher,
                                 @ApiIgnore @SsoStudent Student student) {
        Long teacherId = teacher != null ? teacher.getId() : null;
        Long studentId = student != null ? student.getId() : null;
        MyGrades myGrades = gradeService.getMyGrades(myGradesParam, teacherId, studentId);
        return myGrades != null ? PtpResultUtils.ok(myGrades) : PtpResultUtils.error(500, "获取成绩出错");
    }


    @ApiOperation("根据任务ID获取评分项")
    @GetMapping("/task/{taskId}/gradeItems")
    public PtpResult getGradeItemByTaskId(@PathVariable Long taskId) {
        List<TaskGradeItem> gradeItems = taskService.getTaskGradeItemsByTaskId(taskId);
        if (gradeItems.size() > 0) {
            return PtpResultUtils.ok(gradeItems);
        } else {
            return PtpResultUtils.error(500, "获取评分项失败");
        }
    }


    //    添加一个gradeRole
    @ApiOperation("添加一个学生信息")
//    @PostMapping("/grade")
    public PtpResult addGradeRole(GradeRole gradeRole) {
        try {
            System.out.println("进入service");
            PtpResult ptpResult = apiGradeService.addGrade(gradeRole);
            System.out.println("进入service后");
            return PtpResultUtils.ok(ptpResult);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }


    //    根据id删除学生的成绩
    @ApiOperation("根据id删除学生的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "gradeId")
    })
//    @DeleteMapping("/grade/{id}")
    public PtpResult deleteById(@PathVariable Long id) {
        try {
            PtpResult ptpResult = apiGradeService.deleteById(id);
            return PtpResultUtils.ok(ptpResult);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    //    更改学生的成绩
    @ApiOperation("根据id更改学生的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "gradeId")
    })
//    @PutMapping("/gradeRole/{id}")
    public PtpResult updateGradeRole(@PathVariable Long id, GradeRole gradeRole) {
        try {
            PtpResult ptpResult = apiGradeService.update(gradeRole);
            return PtpResultUtils.ok(ptpResult);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("老师给学生打分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/student/grade")
    public PtpResult postGrade(@ApiIgnore @SsoTeacher Teacher teacher,
                               @RequestBody GradeItem gradeItem) {

        boolean success = gradeService.postGrade(teacher, gradeItem);
        return success ? PtpResultUtils.ok() : PtpResultUtils.error(499, "评分失败");
    }

    @ApiOperation("学生获取自己对于某个任务的评分情况")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @GetMapping("/student/grade/task/{taskId}")
    public PtpResult gradeInfo(@ApiIgnore @SsoStudent Student student,
                               @PathVariable Long taskId) {

        GradeInfo gradeInfo = apiGradeService.getMyGradeInfo(student, taskId);
        return gradeInfo != null ? PtpResultUtils.ok(gradeInfo) : PtpResultUtils.error(499, "评分失败");
    }


    @ApiOperation("学生给老师评教")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header")
    })
    @PostMapping("/student/evaluatingTeacher")
    public PtpResult evaluatingTeacher(@ApiIgnore @SsoStudent Student student,
                                       @RequestBody GradeTeacher grade) {
        boolean success = apiGradeService.addEvaluatingTeacher(student.getId(), grade.getScore(), grade.getTaskId());
        if (success) {
            return PtpResultUtils.ok("打分成功!");
        } else {
            return PtpResultUtils.error(500, "打分失败!");
        }
    }

    @ApiOperation("学生查询某个任务的评教分数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户信息key值", paramType = "header"),
            @ApiImplicitParam(name = "taskId", value = "任务Id")
    })
    @GetMapping("/student/evaluatingTeacher/task/{taskId}")
    public PtpResult evaluatingTeacher(@ApiIgnore @SsoStudent Student student,
                                       @PathVariable Long taskId) {
        GradeTeacher grade = gradeService.findGradeTeacherByStudentIdAndTaskId(student.getId(), taskId);
        if (grade != null) {
            return PtpResultUtils.ok(grade);
        } else {
            return PtpResultUtils.ok();
        }
    }

    //  添加一个学生的信息
    @ApiOperation("添加一个学生的信息")
//    @PostMapping("/grade/init")
    public PtpResult addGrade(Grade grade) {
        try {
            gradeService.insertGrade(grade);
            return PtpResultUtils.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    //    删除一个学生的信息
    @ApiOperation("删除一个学生的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "gradeRoleId")
    })
//    @DeleteMapping("/grade/init/{id}")
    public PtpResult deleteGradeById(@PathVariable Long id) {
        try {
            gradeRoleService.deleteByGradeId(id);
            gradeService.deleteById(id);
            return PtpResultUtils.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }

    }

    //    查找所有学生成绩
    @ApiOperation("查找所有学生的信息")
//    @GetMapping("/grade")
    public PtpResult findAll() {
        try {
            InputStream all = apiGradeService.findAll();
            return PtpResultUtils.ok(all);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(533);
        }

    }

}
